package com.cf.controller;

import com.cf.entity.Advert;
import com.cf.entity.Item;
import com.cf.entity.Result;
import com.cf.service.AdvertService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class AdvertController {
    @Autowired
    private AdvertService advertService;

    //查找
    @RequestMapping("/selectAdvert")
    public Object selectAdvert( @RequestParam(defaultValue = "1") int pagenum,@RequestParam(defaultValue = "10") int pagesize,String iname) {
        PageInfo<Advert> adverts = advertService.selectAdvert(iname,pagenum,pagesize);
        if (adverts!=null) {
            return new Result(200, "查询成功", adverts);
        }else {
            return new Result(400, "没有数据", null);
        }
    }

    //逻辑删除
    @RequestMapping("/deletetAdvert")
    public Object deletetAdvert(long adid) {
        int i = advertService.deletetAdvert(adid);
        if (i != 0) {
            return new Result(200, "删除成功", 0);
        }else {
            return new Result(400, "删除失败", 1);
        }
    }

    //未通过
    @RequestMapping("/selectAdvertIsdel")
    public Object selectAdvertIsdel(@RequestParam(defaultValue = "1") int pagenum,@RequestParam(defaultValue = "3") int pagesize,String iname) {
        PageInfo<Advert> pageInfo = advertService.selectAdvertIsdel(iname,pagenum, pagesize);
        if (pageInfo.getList().size() != 0) {
            return new Result(200, "查询成功", pageInfo);
        }else {
            return new Result(400, "查询失败", null);
        }
    }

    //通过
    @RequestMapping("/deletetAdvertIsdel")
    public Object deletetAdvertIsdel(long adid) {
        Date starttime  = new Date();
        long iStartTime = starttime.getTime();
        //20是天数，可以动态获取
        long endTime =iStartTime+(20*24*60*60*1000);
        Date endtime = new Date(endTime);
        System.out.println(iStartTime+"----"+endTime);
        int i = advertService.deletetAdvertIsdel(starttime,endtime,adid);
        if (i != 0) {
            return new Result(200, "成功", 0);
        }else {
            return new Result(400, "失败", 0);
        }
    }

    //添加数据
    @RequestMapping("/insertAdvert")
    public Object insertAdvert(Advert advert) {
        int i = advertService.insertAdvert(advert);
        if (i != 0) {
            return new Result(200, "添加成功", 0);
        }else {
            return new Result(400, "添加失败", 0);
        }
    }
    //真逻辑查询
    @RequestMapping("/selectByStatus")
    public Object selectByStatus(@RequestParam(defaultValue = "1") int pagenum,@RequestParam(defaultValue = "3") int pagesize){
        PageInfo<Advert> adverts = advertService.selectByStatus(pagenum,pagesize);
        if (adverts.getList().size() != 0) {
            return new Result(200, "查询成功", adverts);
        }else {
            return new Result(400, "没有数据", null);
        }
    }
    //真逻辑查询
    @RequestMapping("/selectItemByAdvert")
    public Object selectItemByAdvert(){
        List<Item> items = advertService.selectItemByAdvert();
        if (items != null) {
            return new Result(200, "添加成功", items);
        }else {
            return new Result(400, "添加失败", null);
        }
    }
}

